from appium import webdriver
#-----------------------------包名相关信息-----------------
desired_caps = {'platformName': 'Android',
                'deviceName': 'Y15QKBPR226LU',
                'platformVersion': '8.0',
                'appPackage': 'com.zhibo.test',
                'appActivity': 'com.ssf.tckotlin.ui.activity.welcome.WelcomeActivity'
                }
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
driver.implicitly_wait(5)
print('start App')
#-----------------------------------------------------------

#---------------------------定义函数------------------------
# 函数isElement，作用：判断页面元素是否存在，如果页面存在元素xx，则进行操作，不存在进行下一步
def isElement(id):
    try:
        driver.find_element_by_id(id)
        stateOne = True
    except:
        stateOne = False
    if stateOne == True:
        driver.find_element_by_id(id).click()
#函数swipeToUp，作用：滑动，向上滑动
def swipeToUp(during):
    width = driver.get_window_size()['width']
    height = driver.get_window_size()['height']
    driver.swipe(width / 2, height * 3 / 4, width / 2, height / 4, during)

#函数swipeTodown，作用：滑动，向下滑动
def swipeToDown(during):
    width = driver.get_window_size()['width']
    height = driver.get_window_size()['height']
    driver.swipe(width / 2, height / 4, width / 2, height * 3 / 4, during)

#-----------------------------------------------------------

# 判断是否有公告
driver.implicitly_wait(8)
isElement('btn_cancel')
print('如果有公告，点击公告')
# 判断是否已经登录
try:
    driver.find_element_by_xpath('//android.widget.TextView[contains(@text, "我的")]')
    stateTwo = True
except:
    stateTwo = False
print('如果已经登录，推出重新登录')
# stateTwo 为真，说明已经登录，直接进入直播间
if stateTwo == True:
    driver.find_element_by_xpath('//android.widget.TextView[contains(@text, "我的")]').click()
    # 退出登录
    #滑动屏幕到底部
    driver.implicitly_wait(3)
    swipeToUp(200)
    swipeToUp(200)
    driver.find_element_by_id('ll_setting').click()
    driver.find_element_by_id('setting_out').click()
    driver.find_element_by_id('dialog_tv_ensure').click()

# stateTwo 为假，说明没有登录，需要进行登录操作
# 登录
# 判断输入框中是否存在文字，如果存在需要点击清除按钮
print('输入账号密码')
isElement('tv_user_name')
isElement('log_username_iv')
driver.find_element_by_id('log_username_et').click()
driver.find_element_by_id('log_username_et').clear()
driver.find_element_by_id('log_username_et').send_keys('13333333333')
driver.find_element_by_id('log_psd_et').click()
driver.find_element_by_id('log_psd_et').clear()
driver.find_element_by_id('log_psd_et').send_keys('111111')
driver.find_element_by_id('log_log_btn').click()

# 判断是否有公告
driver.implicitly_wait(5)
isElement('btn_cancel')
#进入直播模块
try:
    driver.find_element_by_xpath('//android.widget.TextView[contains(@text, "直播")]').click()
except:
    stateTwo = False
# 点击直播模式，进入直播列表，进入直播房间
print('进入直播')
driver.find_element_by_id('rb_live_mode').click()
print('进入房间')
driver.find_element_by_id('iv_item_buylott_live_cover').click()

# 页面是否存在新手教程，存在跳过新手教程
isElement('iv_skip')
driver.implicitly_wait(3)
driver.find_element_by_id('history_btn').click()
driver.find_element_by_xpath('//android.widget.TextView[contains(@text, "投注记录")]').click()
driver.find_element_by_xpath('//android.widget.TextView[contains(@text, "开奖记录")]').click()
driver.find_element_by_id('iv_close').click()
#直播界面三个按钮开关
print('操作直播界面三个按钮')
driver.find_element_by_id('iv_audience_effect').click()
driver.find_element_by_id('iv_audience_effect').click()
driver.find_element_by_id('iv_lottery_play_info').click()
driver.find_element_by_id('iv_close').click()
driver.find_element_by_id('iv_audience_msg').click()
driver.implicitly_wait(3)
driver.find_element_by_id('et_audience_msg').send_keys('test123')
driver.find_element_by_id('btn_audience_send').click()
#
#----------------------------调试到这里
# driver.find_element_by_id('iv_audience_close').click()
# driver.find_element_by_id('tv_betting_history').click()
# driver.find_element_by_id('tv_lottery_history').click()
# driver.find_element_by_id('iv_close').click()
# driver.find_element_by_id('rl_audience_method').click()
# driver.find_element_by_link_text('大小单双').click()




